package ift605.tp2.sectionC.client;

import java.rmi.RemoteException;
import java.rmi.registry.LocateRegistry;
import java.rmi.registry.Registry;
import java.rmi.server.UnicastRemoteObject;
import java.util.Scanner;

import udes.ds.rmi.hw.Constant;
import udes.ds.rmi.hw.Equation;
import udes.ds.rmi.hw.PolynomialEquation;
import udes.ds.rmi.hw.UnknownEquationTypeException;

import ift605.tp2.sectionA.server.MyServerPublicInterface;
import ift605.tp2.sectionC.server.MyUnknownFailSafeInterface;

@SuppressWarnings("serial")
public class Marvin extends UnicastRemoteObject implements UnknownEquationUserInterface{

	protected Marvin() throws RemoteException {
		super();
	}

	public static void main(String[] args) throws RemoteException {
		Marvin client = new Marvin();
		client.doSomething();
	}

	private void doSomething() {
		MyUnknownFailSafeInterface proxy = null;
	    Scanner in = new Scanner(System.in);
	    System.out.println("Enter ipAddr of RMI Registry");
		String ipAddr = in.nextLine();
	    
		if(ipAddr == null || ipAddr.isEmpty())
		{
			ipAddr = "localhost";
		}
		
	    try {
	    	Registry registry = LocateRegistry.getRegistry(ipAddr);
			proxy = (MyUnknownFailSafeInterface) registry.lookup("MyServer");
		} catch (Exception e) {
			e.printStackTrace();
			return;
		}
	    
	    Equation eq = null;
	    
	    try 
	    {
		    do
		    {
		    	eq = GetEquation(in);
		    	
		    	if(eq != null)
		    	{		    		
					Equation result = proxy.resolveEquation(eq, (UnknownEquationUserInterface)this);
					if( result == null)
					{
						System.out.println("Your request have been kicked by an admin");
					}
					else
					{
						System.out.println("Answer is " + result.getUserReadableString()  + " !!! Awesome !!!");
					}
				}
		    }
		    while(eq != null);
	    } 
	    catch (RemoteException e) 
	    {
			e.printStackTrace();
		}
		
		in.close();
	}
	
	private Equation GetEquation(Scanner in)
	{
		Equation eq = null;
		boolean finished = false;
		while(!finished)
		{
			System.out.println("Choose command :");
			System.out.println("(1) : Answer to life and universe and everything");
			System.out.println("(2) : Polynome of <Answer to life and universe and everything>");
			System.out.println("(3) : Quit");
			
			int reading= in.nextInt();

			switch(reading)
			{
			case 1:
			{
				eq = new MyEquation();
				finished = true;
				break;
			}
			case 2:
			{
				eq = new PolynomialEquation(new MyEquation(), new Constant(42));
				finished = true;
				break;
			}
			case 3:
				finished = true;
				break;
			default:
				break;
			}
		}
		
		return eq;
	}

	@Override
	public Equation resolveUnknownEquation(Equation aEquation)
			throws RemoteException, UnknownEquationTypeException {
		
		if(aEquation.getClass() == MyEquation.class)
		{
			return new Constant( ((MyEquation)aEquation).getAnswerOfUniverse());
		}
		else
		{
			throw new UnknownEquationTypeException("This is not a MyEquation equation, so Marvin cannot derive it");
		}
	}
}
